package com.hymjweb.demo.hymj.finance_manage.pay.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.finance_manage.pay.bean.CwBillPayClearedPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface CwPayMapper {



    class CwPayMapperProvider{

        public String findAll(Map map) {
            String sql= new SQL() {{
                SELECT("t.* ");
                FROM("cw_bill_pay_cleared t where 1 = 1 ");
            }}.toString();

            if(map!=null){

                String keyBillId= DataTypeUtil.toStringObject(map.get("keyBillId"));
                Long keyBillTypeId = DataTypeUtil.toLongObject(map.get("keyBillTypeId"));
                Long keyBillPayClearedTypeId = DataTypeUtil.toLongObject(map.get("keyBillPayClearedTypeId"));

                String keyClearedStartDate = DataTypeUtil.toNotNullString(map.get("keyClearedStartDate"));
                String keyClearedEndDate = DataTypeUtil.toNotNullString(map.get("keyClearedEndDate"));

                String czyId = DataTypeUtil.toStringObject(map.get("czyId"));
                String departmentId = DataTypeUtil.toStringObject(map.get("departmentId"));

                if(keyBillId!=null){
                    sql += " and t.bill_id=" + DataTypeUtil.quoteStr(keyBillId);
                }

                if(keyBillTypeId!=null){
                    sql += " and t.bill_type_id=" + keyBillTypeId;
                }
                if(keyBillPayClearedTypeId!=null){
                    sql += " and t.bill_pay_cleared_type_id=" + keyBillPayClearedTypeId;
                }


                if(!"".equals(keyClearedStartDate)){
                    if(!"".equals(keyClearedEndDate)){
                        int d=keyClearedStartDate.compareTo(keyClearedEndDate);
                        if(d>0){
                            String tempDate = keyClearedStartDate;
                            keyClearedStartDate = keyClearedEndDate;
                            keyClearedEndDate = tempDate;
                        }
                        sql += " and (date_format(t.create_time,'%Y-%m-%d') between "+ DataTypeUtil.quoteStr(keyClearedStartDate) +" and " + DataTypeUtil.quoteStr(keyClearedEndDate) + ")";
                    }
                    else{
                        sql += " and date_format(t.create_time,'%Y-%m-%d') >= "+ DataTypeUtil.quoteStr(keyClearedStartDate);
                    }
                }
                else{
                    if(!"".equals(keyClearedEndDate)){
                        sql += " and date_format(t.create_time,'%Y-%m-%d') <= "+ DataTypeUtil.quoteStr(keyClearedEndDate);
                    }
                }
            }

            return sql;
        }

    }


    @Results(id = "cwBillPayClearedPojoPojoMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "bill_id", property = "billId"),
            @Result(column = "bill_type_id", property = "billTypeId"),
            @Result(column = "bill_pay_cleared_type_id", property = "billPayClearedTypeId"),
            @Result(column = "bill_pay_cleared_amount", property = "billPayClearedAmount"),

            @Result(column = "create_time", property = "createTime"),
            @Result(column = "create_user_id", property = "createUserId"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "update_user_id", property = "updateUserId")
    })
    @SelectProvider(type = CwPayMapperProvider.class, method = "findAll")
    List<CwBillPayClearedPojo> findAll(Map param);


    @Insert(
            "<script>" +
                    "insert into cw_bill_pay_cleared(id,bill_id,bill_type_id,bill_pay_cleared_type_id,bill_pay_cleared_amount,create_time,create_user_id,update_time,update_user_id) values " +
                    "<foreach collection='cwBillPayClearedPojos' item='pojo' separator=','>" +
                    "(#{pojo.id},#{pojo.billId},#{pojo.billTypeId},#{pojo.billPayClearedTypeId},#{pojo.billPayClearedAmount},#{pojo.createTime},#{pojo.createUserId},#{pojo.updateTime},#{pojo.updateUserId})" +
                    "</foreach>" +
            "</script>"
    )
    @Options(keyProperty = "id")
    int batchInsert(@Param("cwBillPayClearedPojos") List<CwBillPayClearedPojo> cwBillPayClearedPojos);

}
